<?php

namespace backend\controllers;

use Yii;
use backend\models\PlanData;
use backend\models\PlanDataSearch;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\UploadForm;
use yii\web\UploadedFile;

/**
 * PlanDataController implements the CRUD actions for PlanData model.
 */
class PlanDataController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all PlanData models.
     * @return mixed
     */
    public function actionIndex()
    {

        # 年度计划完成量
        $complete = PlanData::find()->orderBy('id DESC')->select('complete')->scalar();
        $plan_data = [
            json_encode([
                'value' => $complete,
                'name' => '已完成金额（万）'
            ]),
            json_encode([
                'value' => 1000 - $complete,
                'name' => '未完成金额（万）'
            ])
        ];
        return $this->render('index', [
            'plan_data' => json_encode($plan_data, JSON_UNESCAPED_UNICODE),
        ]);
    }

    /**
     * Displays a single PlanData model.
     * @param integer $id
     * @return mixed
     */
    public function actionView()
    {
        $searchModel = new PlanDataSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('view', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider
        ]);
    }

    /**
     * Creates a new PlanData model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new PlanData();
        $uploadForm = new UploadForm();
        $uploadForm->imageFile = UploadedFile::getInstance($uploadForm, 'imageFile');
        $imageUrl = '';
        if ($uploadForm->imageFile) {
            $imageUrl = $uploadForm->upload();
        }
        if ($model->load(Yii::$app->request->post()) && $model->create($imageUrl)) {
            Yii::$app->getSession()->setFlash('success', '添加成功');
            return $this->redirect('view');
        } else {
            Yii::$app->getSession()->setFlash('warning', '添加失败');
            return $this->redirect('view');
        }

    }

    /**
     * Updates an existing PlanData model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate()
    {
        $model = new PlanData();
        $uploadForm = new UploadForm();
        $uploadForm->imageFile = UploadedFile::getInstance($uploadForm, 'imageFile');
        $imageUrl = '';
        if ($uploadForm->imageFile) {
            $imageUrl = $uploadForm->upload();
        }
        if ($model->load(Yii::$app->request->post()) && $model->edit($imageUrl)) {
            Yii::$app->getSession()->setFlash('success', '修改成功');
            return $this->redirect('view');
        } else {
            Yii::$app->getSession()->setFlash('warning', '修改失败');
            return $this->redirect('view');
        }
    }

    /**
     * Deletes an existing PlanData model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the PlanData model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return PlanData the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = PlanData::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionShow()
    {
        #合同图片
        $contract_img = PlanData::find()->where(['id' => Yii::$app->request->post('contract_id')])->select('contract_img')->scalar();

        return $contract_img;
    }

    public function actionGetData()
    {
        # 年度计划完成量
        $complete = PlanData::find()->orderBy('id DESC')->select('complete')->scalar();
        $plan_data = [
            json_encode([
                'value' => $complete,
                'name' => '已完成金额（万）'
            ]),
            json_encode([
                'value' => 1000 - $complete,
                'name' => '未完成金额（万）'
            ])
        ];
        return json_encode($plan_data, JSON_UNESCAPED_UNICODE);

    }
}
